/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Assets;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author mishra
 */
public class EditAssets extends HttpServlet {

    HashMap hm=new HashMap();
    File fi=null;
    AssetsBean ab=new AssetsBean();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        int k=0;
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
        try {
            ServletFileUpload servlet = new ServletFileUpload();
                FileItemIterator fit = servlet.getItemIterator(request);
                while (fit.hasNext()) {
                    FileItemStream f = fit.next();
                    if (f.isFormField()) {
                        InputStream input = f.openStream();
                        byte b[] = new byte[input.available()];
                        input.read(b);
                        String value = new String(b);
                        hm.put(f.getFieldName(), value);
                    } else {
                        if (f.getName().isEmpty()) {
                            continue;
                        } else if(f.getFieldName().equals("picture")) { 
                            String fileName = (String) hm.get("sno");
                            String postfix = "p";

                            String s = DocUpload.FileUpload.writeFile(f, fileName + postfix);
                            ab.setPictureName(s);
                            
                        
                        }else if(f.getFieldName().equals("attach")){
                            String fileName = (String) hm.get("sno");
                            String postfix = "a";

                            String s = DocUpload.FileUpload.writeFile(f, fileName + postfix);
                            ab.setAttachmentName(s);
                        }
                    }
                }
        }catch(Exception e){
            
        }
        } 
        try{
            ab.setAssetName(hm.get("aname").toString());
            if(Integer.parseInt(hm.get("asgn").toString())==1)
            {
                ab.setAssinedStatus(Integer.parseInt(hm.get("asgn").toString()));
                ab.setEmployeeId(Integer.parseInt(hm.get("assemp").toString()));
                String asdate[]=hm.get("adate").toString().split("/");
                if(asdate.length==3){
                Date asignDate=new Date(Integer.parseInt(asdate[2])-1900, Integer.parseInt(asdate[1])-1, Integer.parseInt(asdate[0]));
                ab.setAssignedDate(asignDate);
                }
            }else{
                ab.setAssinedStatus(Integer.parseInt(hm.get("asgn").toString()));
                ab.setEmployeeId(0);
                ab.setAssignedDate(null);
            }
            ab.setBrandId(Integer.parseInt(hm.get("brand").toString()));
            ab.setVendorId(Integer.parseInt(hm.get("vendor").toString()));
            ab.setSerialNo(hm.get("sno").toString());
            
            String acdate[]=hm.get("accqdate").toString().split("/");
            if(acdate.length==3){
            Date acquarDate=new Date(Integer.parseInt(acdate[2])-1900, Integer.parseInt(acdate[1])-1, Integer.parseInt(acdate[0]));
            ab.setAccquireDate(acquarDate);
            }
            String wstartdate[]=hm.get("warstart").toString().split("/");
            if(wstartdate.length==3){
            Date warstartDate=new Date(Integer.parseInt(wstartdate[2])-1900, Integer.parseInt(wstartdate[1])-1, Integer.parseInt(wstartdate[0]));
            ab.setWarrantyStart(warstartDate);
            }
            String wenddate[]=hm.get("warend").toString().split("/");
            if(wenddate.length==3){
            Date warendDate=new Date(Integer.parseInt(wenddate[2])-1900, Integer.parseInt(wenddate[1])-1, Integer.parseInt(wenddate[0]));
            ab.setWarrantyEnd(warendDate);
            }
            ab.setModelNo(hm.get("modelno").toString());
            ab.setDescription(hm.get("desc").toString());
            ab.setDepartmentId(Integer.parseInt(hm.get("depart").toString()));
            ab.setUserId(Integer.parseInt(session.getAttribute("uid").toString()));
            ab.setAssetId(Integer.parseInt(hm.get("assid").toString()));
            
            k=Assets.Query.edit(ab);
             if (k != 0) {
                    session.setAttribute("status", "Success");
                    session.setAttribute("msg", "Assets "+ab.getAssetName()+" Details Updated Successfully.");
                    response.sendRedirect("/RIT/Services/Assets/AddAssets.jsp");
                } else {
                    session.setAttribute("status", "Fail");
                    session.setAttribute("msg", "Assets "+ab.getAssetName()+" Details not Updated");
                    response.sendRedirect("/RIT/Services/Assets/AddAssets.jsp");
                }
            
        }catch(Exception e){
             session.setAttribute("status", "Fail");
            session.setAttribute("msg", "Assets Details not Updated.(e)");
            response.sendRedirect("/RIT/Services/Assets/AddAssets.jsp");
        }
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
